define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'qingdongams/jibie/index' + location.search,
                    add_url: 'qingdongams/jibie/add',
                    edit_url: 'qingdongams/jibie/edit',
                    del_url: 'qingdongams/jibie/del',
                    multi_url: 'qingdongams/jibie/multi',
                    import_url: 'qingdongams/jibie/import',
                    table: 'qingdongams_jibie',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'jigou_id', title: __('Jigou_id')},
                        {field: 'qianzai_tian', title: __('Qianzai_tian')},
                        {field: 'putong_tian', title: __('Putong_tian')},
                        {field: 'xiaoke_tian', title: __('Xiaoke_tian')},
                        {field: 'gao_jine', title: __('Gao_jine')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'jigou.jigou_name', title: __('Jigou.jigou_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'qingdongams/jibie/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'qingdongams/jibie/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'qingdongams/jibie/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {

            show();
            $(document).on('input', 'input', function (data) {
                show();
            });
            Controller.api.bindevent();

            function show(){
                var gao_jine = $("input[name='row[gao_jine]']").val();
                var qianzai_tian = $("input[name='row[qianzai_tian]']").val();
                var putong_tian = $("input[name='row[putong_tian]']").val();
                var xiaoke_tian = $("input[name='row[xiaoke_tian]']").val();

                Fast.api.ajax({
                    url: 'qingdongams/jibie/getcustomernum',
                    data: {
                        gao_jine: gao_jine,
                        qianzai_tian: qianzai_tian,
                        putong_tian: putong_tian,
                        xiaoke_tian: xiaoke_tian,
                    },
                    loading: false
                }, function (data, res) {
                    console.log(data, res)
                    $(".qianzai").text(res.data.qianzai);
                    $(".xin").text(res.data.xin);
                    $(".yiban").text(res.data.yiban);
                    $(".zhongyao").text(res.data.zhongyao);
                    $(".chenshui").text(res.data.chenshui);
                    return false;
                }, function () {
                    return false;
                });
            }
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
